package com.ruoyi.system.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JudgeResultObject {
    private Long id;
    private String systemName;

    private float weight = 1.0f;
    private List<Indicator> children;


    private Map<Long, Double> resultMap;

    // Getters and setters
    public Long getId() { return id; }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public Map<Long, Double> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<Long, Double> resultMap) {
        this.resultMap = resultMap;
    }

    public void setId(Long id) { this.id = id; }
    public String getSystemName() { return systemName; }
    public void setSystemName(String systemName) { this.systemName = systemName; }
    public List<Indicator> getChildren() { return children; }
    public void setChildren(List<Indicator> children) { this.children = children; }

    public static JudgeResultObject parseFromJson(String json) {
        return JSON.parseObject(json, new TypeReference<JudgeResultObject>() {});
    }
    public String toJsonString() {
        return JSON.toJSONString(this);
    }

    public void calculateWeight(HashMap<Long, Double> weightMap) {
        children.get(0).calculateWeight(null, 0, weightMap);
    }

    public void setSubjectWeight(Map<Long, Double> map){
        children.get(0).setSubjectWeight(map);
    }
}

class Indicator {
    private Long id;
    private String indicatorName;
    private JudgementMatrix judgementMatrix;
    private Long parentId;

    private List<Indicator> children;

    private List<Double> weightVector;

    private double weight = 1.0f;
    public double getWeight() { return weight; }
    public void setWeight(double weight) { this.weight = weight; }

    public List<Indicator> getChildren() { return children; }

    public void setChildren(List<Indicator> children) {
        this.children = children;
    }

    public Long getParentId() { return parentId; }
    public void setParentId(Long parentId) { this.parentId = parentId; }
    // Getters and setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getIndicatorName() { return indicatorName; }
    public void setIndicatorName(String indicatorName) { this.indicatorName = indicatorName; }
    public JudgementMatrix getJudgementMatrix() { return judgementMatrix; }
    public void setJudgementMatrix(JudgementMatrix judgementMatrix) { this.judgementMatrix = judgementMatrix; }

    public String toString() {
        return JSON.toJSONString(this);
    }
    public void calculateWeight(Indicator parent, int n, HashMap<Long, Double> weightMap) {
        System.out.println("hqyTest"+this.getIndicatorName());
        if(this.judgementMatrix == null && this.getChildren() != null){
            this.judgementMatrix = new JudgementMatrix();
            this.judgementMatrix.setData(createOnesMatrix(children.size(), children.size()));
        }
        if (parent != null) {
            this.setWeight(parent.getWeight() * parent.weightVector.get(n));
            weightMap.put(this.getId(), this.getWeight());
        } else {
            this.setWeight(1.0f); // 根节点的权重默认为 1.0
        }
        if(this.getChildren() == null){
            return;
        }
        this.weightVector = this.judgementMatrix.processJudgementMatrix();
        int i=0;
        for (Indicator child : this.getChildren()) {
            child.calculateWeight(this, i++, weightMap);
        }
    }
    public void setSubjectWeight(Map<Long, Double> map){
        System.out.println("HQYTEST"+ this.getIndicatorName());
        if(map.get(this.getId()) != null){
            this.weight = map.get(this.getId());
        }
        else {
            this.weight = 1.0f;
        }
        if(this.getChildren() != null){
            for (Indicator child : this.getChildren()) {
                child.setSubjectWeight(map);
            }
        }
    }

    public static List<List<Double>> createOnesMatrix(int rows, int cols) {
        List<List<Double>> matrix = new ArrayList<>(rows);

        for (int i = 0; i < rows; i++) {
            List<Double> row = new ArrayList<>(cols);
            for (int j = 0; j < cols; j++) {
                row.add(1.0);
            }
            matrix.add(row);
        }

        return matrix;
    }

    public static void main(String[] args) {
        int rows = 5;
        int cols = 4;
        List<List<Double>> onesMatrix = createOnesMatrix(rows, cols);

        // 打印矩阵以验证结果
        for (List<Double> row : onesMatrix) {
            for (Double value : row) {
                System.out.printf("%.1f ", value);
            }
            System.out.println();
        }
    }
}

class JudgementMatrix {
    private List<List<Double>> data;
    private List<String> colHeader;
    private List<String> rowHeader;
    public List<Double> processJudgementMatrix() {
        int rows = data.size();
        int cols = data.get(0).size();

        // 步骤 1: 计算列总和和归一化矩阵
        List<List<Double>> normalizedMatrix = getNormalizedMatrix(data, cols, rows);

        // 步骤 3: 按行求和
        List<Double> rowSums = new ArrayList<>(rows);
        for (List<Double> row : normalizedMatrix) {
            double sum = 0.0;
            for (Double value : row) {
                sum += value;
            }
            rowSums.add(sum);
        }

        // 步骤 4: 计算权重向量
        List<Double> weightVector = new ArrayList<>(rows);
        for (double sum : rowSums) {
            weightVector.add(sum / cols);
        }

        return weightVector;
    }

    private static List<List<Double>> getNormalizedMatrix(List<List<Double>> matrix, int cols, int rows) {
        List<Double> columnSums = new ArrayList<>(cols);
        for (int j = 0; j < cols; j++) {
            double sum = 0.0;
            for (int i = 0; i < rows; i++) {
                sum += matrix.get(i).get(j);
            }
            columnSums.add(sum);
        }

        // 步骤 2: 归一化矩阵
        List<List<Double>> normalizedMatrix = new ArrayList<>(rows);
        for (int i = 0; i < rows; i++) {
            List<Double> row = new ArrayList<>(cols);
            for (int j = 0; j < cols; j++) {
                double value = matrix.get(i).get(j) / columnSums.get(j);
                row.add(value);
            }
            normalizedMatrix.add(row);
        }
        return normalizedMatrix;
    }


    // Getters and setters
    public List<List<Double>> getData() { return data; }
    public void setData(List<List<Double>> data) { this.data = data; }
    public List<String> getColHeader() { return colHeader; }
    public void setColHeader(List<String> colHeader) { this.colHeader = colHeader; }
    public List<String> getRowHeader() { return rowHeader; }
    public void setRowHeader(List<String> rowHeader) { this.rowHeader = rowHeader; }
}
